package com.io.example.demoparent.mq.topic;


import com.io.example.demoparent.mq.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/***
 *  主题模式
 */
public class ReceiveLogsTopic02 {


    private static final String EXCHANGE_NAME = "topic_logs";


    public static void main(String[] args) throws IOException, TimeoutException {
        TopicConstructor02();
    }

    public static  void TopicConstructor02() throws IOException, TimeoutException {
        Channel rabbitChannel = RabbitMQUtil.getRabbitChannel( );
        rabbitChannel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        rabbitChannel.queueDeclare("Q1",false,false,false,null);
        rabbitChannel.queueBind("Q1",EXCHANGE_NAME,"*.orange.*");

        rabbitChannel.basicConsume("Q1",Boolean.TRUE, new DeliverCallback( ) {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                System.out.println("接收成功的消息："+ new String( delivery.getBody(), StandardCharsets.UTF_8)+"----"+"绑定键"+delivery.getEnvelope().getRoutingKey());
            }
        }, new CancelCallback( ) {
            @Override
            public void handle(String s) throws IOException {

            }
        });



    }
}
